package com.kaoyaya.util;

import java.io.IOException;

import org.json.JSONObject;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaoyaya.delivery.activity.R;

/**
 * 通用工具类
 * @date 2013-4-23
 * @author Goven
 * @email gxl3999@gmail.com
 */
public class CommUtil {
	
	/**
	 * 创建并显示进度对话框
	 * @param context 上下文
	 * @param title 标题
	 * @param message 信息
	 * @param indeterminate 是否确定
	 * @param cancelable 是否可撤销
	 * @return 进度对话框
	 * @author Goven
	 * @date 2013-4-22
	 */
	public static ProgressDialog createProgress(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable) {
		ProgressDialog dialog = new ProgressDialog(context);
		dialog.setTitle(title);
		dialog.setMessage(message);
		dialog.setIndeterminate(indeterminate);
		dialog.setCancelable(false);
		dialog.show();
		return dialog;
	}
	
	/**
	 * 得到自定义的progressDialog 
	 * @param msg 显示的消息
	 * @return 对话框
	 * @author Goven
	 * @date 2013-6-7
	 */
	public static Dialog createLoadingDialog(Context context, String msg) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.llDialogLoading);// 加载布局
		ImageView ivLoading = (ImageView) v.findViewById(R.id.ivLoading);
		TextView tvTip = (TextView) v.findViewById(R.id.tvTip);// 提示文字
		// 加载动画
		Animation animation = AnimationUtils.loadAnimation(context, R.anim.rotate_loading);
		// 使用ImageView显示动画
		ivLoading.startAnimation(animation);
		if (StringUtil.isNotTrimBlank(msg)) {
			tvTip.setText(msg);// 设置加载信息
		}
		Dialog loadingDialog = new Dialog(context, R.style.dialog_loading);// 创建自定义样式dialog
		loadingDialog.setCancelable(false);// 不可以用“返回键”取消
		loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
		loadingDialog.show();
		return loadingDialog;
	}
	
	/**
	 * 更改权限
	 * @param permission 权限
	 * @param path 文件路径
	 * @author Goven
	 * @date 2013-4-23
	 */
	public static void chmod(String permission, String path) {
		try {
			String command = "chmod " + permission + " " + path;
			Runtime runtime = Runtime.getRuntime();
			runtime.exec(command);
		} catch (IOException e) {
			Log.e("CommUtil (chmod) --> ", e.toString());
		}
	}
	
	/**
	 * 字符串转json对象
	 */
	public static JSONObject string2JSON(String str, String split) {
		JSONObject json = new JSONObject();
		try {
			String[] arrStr = str.split(split);
			for (int i = 0; i < arrStr.length; i++) {
				String[] arrKeyValue = arrStr[i].split("=");
				json.put(arrKeyValue[0], arrStr[i].substring(arrKeyValue[0].length() + 1));
			}
		} catch (Exception e) {
			logE("CommUtil(string2JSON)", e.toString());
		}
		return json;
	}
	
	/**
	 * 打印默认info级别的日志
	 * @param tag 标签
	 * @param info 信息
	 * @author Goven
	 * @date 2013-4-23
	 */
	public static void log(String tag, String info) {
		Log.i(tag, info);
	}
	
	/**
	 * 打印debug级别的日志
	 * @param tag 标签
	 * @param info 信息
	 * @author Goven
	 * @date 2013-4-23
	 */
	public static void logD(String tag, String info) {
		Log.d(tag, info);
	}
	
	/**
	 * 打印error级别的日志
	 * @param tag 标签
	 * @param info 信息
	 * @author Goven
	 * @date 2013-4-23
	 */
	public static void logE(String tag, String info) {
		Log.e(tag, info);
	}
	
}
